package com.niiwoo.civet.mobile.controller;/**
 * Created by DELL on 2017/12/16.
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.civet.account.dto.request.SubmitAccountFundTransferRequestDTO;
import com.niiwoo.civet.account.dto.response.SubmitAccountFundTransferReponseDTO;
import com.niiwoo.civet.account.service.AccountFundTransferDubboService;
import com.niiwoo.civet.mobile.vo.request.SubmitAccountFundTransferTempVO;
import com.niiwoo.civet.mobile.vo.request.SubmitAccountFundTransferVO;
import com.niiwoo.civet.mobile.vo.response.SubmitAccountFundTransferResponseVO;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import com.niiwoo.tripod.web.annotation.AuthIgnore;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**账户资金划拨
 * @author xiaoyingping
 * @create 2017-12-15 15:01
 **/
@RestController
@RequestMapping("/account/fundTransfer")
@Api(tags = "账户资金划拨(肖应平)", description = "/account/fundTransfer")
@Slf4j
public class AccountFundTransferController {

    @Reference(version = "1.0.0")
    private AccountFundTransferDubboService accountFundTransferDubboService;

    @PostMapping("/submit")
    @ApiOperation("提交资金划拨请求")
    public Result<SubmitAccountFundTransferResponseVO> submitFundTransfer(@RequestBody @Valid SubmitAccountFundTransferVO submitAccountFundTransferVO) {
        log.info("submitFundTransfer start:{}", submitAccountFundTransferVO);
        String userId = PrincipalContext.getUserId();

        SubmitAccountFundTransferRequestDTO submitAccountFundTransferRequest = new SubmitAccountFundTransferRequestDTO();
        submitAccountFundTransferRequest.setUserId(userId);
        submitAccountFundTransferRequest.setAmount(submitAccountFundTransferVO.getAmount());
        submitAccountFundTransferRequest.setPayAccountUserRole(UserRoleEnum.enumOf(submitAccountFundTransferVO.getPayAccountUserRole()));
        submitAccountFundTransferRequest.setReceiveAccountUserRole(UserRoleEnum.enumOf(submitAccountFundTransferVO.getReceiveAccountUserRole()));
        SubmitAccountFundTransferReponseDTO reponseDTO = accountFundTransferDubboService.submitFundTransfer(submitAccountFundTransferRequest);

        SubmitAccountFundTransferResponseVO responseVO = new SubmitAccountFundTransferResponseVO();
        responseVO.setTransferSuccess(reponseDTO.isTransferSuccess());
        responseVO.setPayAccountBalance(reponseDTO.getPayAccountBalance());
        responseVO.setReceiveAccountBalance(reponseDTO.getReceiveAccountBalance());

        log.info("submitFundTransfer end:{}", submitAccountFundTransferVO);
        return Result.with(responseVO);
    }

    @PostMapping("/fundTransferForBackdoor")
    @ApiOperation("提交资金划拨请求-临时调用")
    @AuthIgnore
    public Result<SubmitAccountFundTransferResponseVO> fundTransferForBackdoor(@RequestBody SubmitAccountFundTransferTempVO submitAccountFundTransferVO) {
        log.info("submitFundTransfer-temp start:{}", submitAccountFundTransferVO);
        String userId = submitAccountFundTransferVO.getUserId();

        SubmitAccountFundTransferRequestDTO submitAccountFundTransferRequest = new SubmitAccountFundTransferRequestDTO();
        submitAccountFundTransferRequest.setUserId(userId);
        submitAccountFundTransferRequest.setAmount(submitAccountFundTransferVO.getAmount());
        submitAccountFundTransferRequest.setPayAccountUserRole(UserRoleEnum.enumOf(submitAccountFundTransferVO.getPayAccountUserRole()));
        submitAccountFundTransferRequest.setReceiveAccountUserRole(UserRoleEnum.enumOf(submitAccountFundTransferVO.getReceiveAccountUserRole()));
        SubmitAccountFundTransferReponseDTO reponseDTO = accountFundTransferDubboService.submitFundTransfer(submitAccountFundTransferRequest);

        SubmitAccountFundTransferResponseVO responseVO = new SubmitAccountFundTransferResponseVO();
        responseVO.setTransferSuccess(reponseDTO.isTransferSuccess());
        responseVO.setPayAccountBalance(reponseDTO.getPayAccountBalance());
        responseVO.setReceiveAccountBalance(reponseDTO.getReceiveAccountBalance());

        log.info("submitFundTransfer-temp end:{}", submitAccountFundTransferVO);
        return Result.with(responseVO);
    }

}
